/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.service;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.thinkgem.jeesite.common.config.Config;
import com.thinkgem.jeesite.common.config.GlobalConst;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.bus.entity.UserOpenid;
import com.thinkgem.jeesite.modules.bus.dao.UserOpenidDao;

/**
 * 系统用户与微信用户关联表Service
 * @author lxy
 * @version 2019-07-25
 */
@Service
@Transactional(readOnly = true)
public class UserOpenidService extends CrudService<UserOpenidDao, UserOpenid> {

	public UserOpenid get(String id) {
		return super.get(id);
	}
	
	public List<UserOpenid> findList(UserOpenid userOpenid) {
		return super.findList(userOpenid);
	}
	
	public Page<UserOpenid> findPage(Page<UserOpenid> page, UserOpenid userOpenid) {
		return super.findPage(page, userOpenid);
	}
	
	@Transactional(readOnly = false)
	public void save(UserOpenid userOpenid) {
		super.save(userOpenid);
		//dao.insert(userOpenid);
		CacheUtils.remove(GlobalConst.PHONE_OPENID_CACHE, GlobalConst.PHONE_OPENID_CACHE_ID_ + userOpenid.getLoginName());
	}
	
	@Transactional(readOnly = false)
	public void delete(UserOpenid userOpenid) {
		super.delete(userOpenid);
		CacheUtils.remove(GlobalConst.PHONE_OPENID_CACHE, GlobalConst.PHONE_OPENID_CACHE_ID_ + userOpenid.getLoginName());
	}
	/**
	 * 通过phoneOpenid删除对象
	 * @param phoneOpenid
	 */
	@Transactional(readOnly = false)
	public void deletePhoneOpenid(UserOpenid userOpenid) {
		dao.deletePhoneOpenid(userOpenid);
		CacheUtils.remove(GlobalConst.PHONE_OPENID_CACHE, GlobalConst.PHONE_OPENID_CACHE_ID_ + userOpenid.getLoginName());
	}
	/**
	 * 通过手机号获得对象
	 * @param loginName
	 * @return
	 */
	public UserOpenid getByLoginName(String loginName) {
		if(StringUtils.isBlank(loginName)) {
			return null;
		}
		UserOpenid phoneOpenid = Config.isDebugMode() ? null : (UserOpenid)CacheUtils.get(GlobalConst.PHONE_OPENID_CACHE, GlobalConst.PHONE_OPENID_CACHE_ID_ + loginName);
		if(phoneOpenid == null){
			phoneOpenid = dao.getByLoginName(loginName);
			if(phoneOpenid == null) {
				return phoneOpenid;
			}
			CacheUtils.put(GlobalConst.PHONE_OPENID_CACHE, GlobalConst.PHONE_OPENID_CACHE_ID_ + loginName, phoneOpenid);
		}
		return phoneOpenid;
	}
	/**
	 * 在登录时保存
	 * @param loginName
	 * @param openId
	 * @throws Exception 
	 */
	@Transactional(readOnly = false)
	public void saveOnLogin(String loginName, String openId) throws Exception {
		if(StringUtils.isBlank(loginName) || StringUtils.isBlank(openId)) {
			throw new Exception("手机号码或者openid为空！");
		}
		UserOpenid userOpenid = new UserOpenid();
		userOpenid.setIsNewRecord(true);
		userOpenid.setId(IdGen.uuid());
		userOpenid.setLoginName(loginName);
		userOpenid.setOpenid(openId);
		this.save(userOpenid);
	}
	/**
	 * 通过openId获得对象
	 * @param openId
	 * @return
	 */
	public UserOpenid getByOpenId(String openId) {
		if(StringUtils.isBlank(openId)) {
			return null;
		}
		return dao.getByOpenId(openId);
	}
}